import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;

public class PathSumEqualToTarget {
    List<List<Integer>> res = new ArrayList<>();
    private int target;

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        this.target = targetSum;
        dfs(root, 0, new ArrayDeque<>());
        return res;

    }

    public void dfs(TreeNode root, int cur, ArrayDeque<Integer> path) {
        if (root == null) return;
        cur += root.val;
        path.add(root.val);
        if (root.left == null && root.right == null && cur == target) {
            res.add(new ArrayList<>(path));
        }
        dfs(root.left, cur, path);
        dfs(root.right, cur, path);
        path.removeLast();

    }

}
